package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.CheckgroupCheckitemDao;
import com.itheima.dao.CheckgroupDao;
import com.itheima.dao.SetmealCheckgroupDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckgroupServiceImpl implements CheckgroupService {

    @Autowired
    private CheckgroupDao checkgroupDao;
    @Autowired
    private CheckgroupCheckitemDao checkgroupCheckitemDao;
    @Autowired
    private SetmealCheckgroupDao setmealCheckgroupDao;

    /**
     * 新增检查组
     * @param checkGroup
     */
    @Transactional
    public void save(CheckGroup checkGroup, String checkitemIds) {
        checkgroupDao.save(checkGroup);
        if(checkitemIds != null && !checkitemIds.equals("")){
            String[] split = checkitemIds.split(",");
            for (String s : split) {
                checkgroupCheckitemDao.insert(checkGroup.getId(), Integer.parseInt(s));
            }
        }
    }

    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkgroupDao.pageQuery(queryPageBean.getQueryString());
        long total = page.getTotal();
        List<CheckGroup> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @Transactional
    public void deleteById(Integer id) {
        Integer count = checkgroupCheckitemDao.getByCheckgroupId(id);
        if(count > 0){
            checkgroupCheckitemDao.deleteByCheckgroupId(id);
        }
        checkgroupDao.delete(id);
    }

    /**
     * 查询检查组
     * @param id
     * @return
     */
    public CheckGroup findById(Integer id) {
        return checkgroupDao.findById(id);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     */
    @Transactional
    public void update(CheckGroup checkGroup, String checkitemIds) {
        checkgroupDao.update(checkGroup);

        Integer checkgroupId = checkgroupCheckitemDao.getByCheckgroupId(checkGroup.getId());
        if(checkgroupId > 0){
            checkgroupCheckitemDao.deleteByCheckgroupId(checkGroup.getId());
        }
        if(checkitemIds != null && !checkitemIds.equals("")){
            String[] split = checkitemIds.split(",");
            for (String s : split) {
                checkgroupCheckitemDao.insert(checkGroup.getId(), Integer.parseInt(s));
            }
        }
    }

    /**
     * 查询所有的检查组
     * @return
     */
    public List<CheckGroup> findAll() {
        return checkgroupDao.findAll();
    }

    /**
     * 查询套餐下的检查组
     * @param setmealId
     * @return
     */
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        return setmealCheckgroupDao.findCheckGroupIdsBySetmealId(setmealId);
    }
}
